package com.lili.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lili.base.vo.ResultAPI;
import com.lili.config.cache.RedisCache;
import com.lili.mapper.QuestionMapper;
import com.lili.pojo.Question;
import com.lili.service.QuestionService;
import com.lili.utils.HTMLUtil;
import com.lili.vo.request.admin.*;
import com.lili.vo.response.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
        implements QuestionService {

    private RedisCache redisCache;

    @Autowired
    public QuestionServiceImpl(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    /**
     * 分页数据
     *
     * @param vo
     * @return
     */
    @Override
    public IPage<QuestionPageRespVO> getPageList(QuestionPageReqVO vo) {
        if (vo.getSubjectIds() != null && vo.getSubjectIds().size() <= 0) {
            vo.setSubjectIds(null);
        }
        IPage<QuestionPageRespVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        IPage<QuestionPageRespVO> pages = baseMapper.getPageList(page, vo);
        if (pages.getPages() < vo.getPageIndex()) {
            page = new Page<>(pages.getPages(), vo.getPageSize());
            pages = baseMapper.getPageList(page, vo);
        }
        pages.getRecords().forEach(item -> {
            item.setQuestionName(HTMLUtil.clear(item.getQuestionName()));
        });
        return pages;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delByIds(List<Integer> ids) {
        if (ids.size() > 0) {
            ids.forEach(item -> redisCache.deleteObject("question:" + item));
            baseMapper.delByIds(ids);
        }
    }


    @Override
    public Question previewQuestion(Integer id) {
        String key = "question:" + id;
        Question question = null;
        Object obj = redisCache.getCacheObject(key);
        if (!Objects.isNull(obj)) {
            question = (Question) obj;
            return question;
        }
        question = baseMapper.selectById(id);
        //新增缓存
        redisCache.setCacheObject(key, question, 10, TimeUnit.MINUTES);
        return question;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultAPI<String> editOrAdd(Question vo) {
        int code = 0;
        String msg = "";
        if (vo.getId() != null && vo.getId() != 0) {
            Question question = this.getBaseMapper().selectById(vo.getId());
            String questionName = question.getQuestionName();
            if (!questionName.equals(vo.getQuestionName())) {
                int num = baseMapper.getNum(vo.getQuestionType(), vo.getQuestionName());
                if (num > 0) {
                    code = 0;
                    msg = "当前科目下同题型题干重复";
                } else {
                    redisCache.deleteObject("question:" + vo.getId());
                    baseMapper.updateById(vo);
                    code = 200;
                    msg = "更新成功";
                }
            } else {
                redisCache.deleteObject("question:" + vo.getId());
                baseMapper.updateById(vo);
                code = 200;
                msg = "更新成功";
            }
            return new ResultAPI<>(code, msg, null);
        } else {
            int num = baseMapper.getNum(vo.getQuestionType(), vo.getQuestionName());
            if (num > 0) {
                code = 0;
                msg = "当前科目下同题型题干重复";
            } else {
                baseMapper.insert(vo);
                code = 200;
                msg = "添加成功";
            }
            return new ResultAPI<>(code, msg, null);
        }
    }


    @Override
    public IPage<AddQuestionPageRespVO> getAddPageList(AddQuestionPageReqVO vo) {
        IPage<AddQuestionPageRespVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        IPage<AddQuestionPageRespVO> pages = baseMapper.getAddPageList(page, vo);
        List<AddQuestionPageRespVO> records = pages.getRecords();
        records.forEach(item -> {
            if (item.getQuestionType() == 1 || item.getQuestionType() == 2) {
                item.setQuestionName(HTMLUtil.clear(item.getQuestionName()));
                item.setAnswerA(HTMLUtil.clear(item.getAnswerA()));
                item.setAnswerB(HTMLUtil.clear(item.getAnswerB()));
                item.setAnswerC(HTMLUtil.clear(item.getAnswerC()));
                item.setAnswerD(HTMLUtil.clear(item.getAnswerD()));
            } else {
                item.setQuestionName(HTMLUtil.clear(item.getQuestionName()));
            }
        });
        pages.setRecords(records);
        return pages;
    }

    /**
     * 每张试卷题目的查询
     *
     * @param examId
     * @return
     */
    @Override
    public List<Question> selectQuestionList(Integer examId) {
        /*List<Question> list = null;
        String key = "exam-question:"+examId;
        Object obj = redisCache.getCacheObject(key);
        if(!Objects.isNull(obj)){
            list = (List<Question>) obj;
            return list;
        }*/
        return baseMapper.selectQuestionListByExamId(examId);
    }
}




